//
//  SplashViewController.swift
//  RFAds_Example
//
//  Created by Roffa Zhou on 2023/2/10.
//  Copyright © 2023 CocoaPods. All rights reserved.
//

import UIKit
import RFAds

class SplashViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        
        openAdVC()
        
        RFAdmobUtils.addSplashLoadedNotif {[weak self] in
            self?.appOpenAdLoaded()
            print("开屏广告加载完")
        }
        RFAdmobUtils.addSplashDismissNotif {[weak self] in
            self?.appOpenAdDidDismissFullScreen()
            print("开屏广告退出")
        }
    }

    func openAdVC(){ //此时不一定能(基本不能)进入广告页, 根据回调再次调用
        RFAdmob.shared.tryToPresentOpenAd(self)
    }
    
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}
extension SplashViewController {
    
    func appOpenAdLoaded() {
        openAdVC()
    }
    func appOpenAdDidDismissFullScreen() {
        
        let mainStoryBoard = UIStoryboard(name: "Main", bundle: nil)
        let mainViewController = mainStoryBoard.instantiateViewController(
          withIdentifier: "MainStoryBoard")
        present(mainViewController, animated: true) {
          self.dismiss(animated: false) {
            // Find the keyWindow which is currently being displayed on the device,
            // and set its rootViewController to mainViewController.
            let keyWindow = UIApplication.shared.windows.first(where: { $0.isKeyWindow })
            keyWindow?.rootViewController = mainViewController
              
              RFAdmob.shared.requestAppOpenAd()  //准备下一个广告，后台到前台展示
          }
        }
        
        
    }
}
